<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Working Examples - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.date.additional.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.date.additional.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.date.constants.html">Constants for General Date Functions</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.date.html">Zend_Date</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.db.html">Zend_Db</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.date.additional" class="section"><div class="info"><h1 class="title">Working Examples</h1></div>
    

    <p class="para">
        Within this chapter, we will describe several additional functions which are also available
        through <span class="classname">Zend_Date</span>. Of course all described functions have additional
        examples to show the expected working and the simple <acronym class="acronym">API</acronym> for the proper
        using of them.
    </p>

    <div class="section" id="zend.date.additional.checking"><div class="info"><h1 class="title">Checking Dates</h1></div>
        

        <p class="para">
            Probably most dates you will get as input are strings. But the problem with strings is
            that you can not be sure if the string is a real date. Therefor
            <span class="classname">Zend_Date</span> has spent an own static function to check date strings.
            <span class="classname">Zend_Locale</span> has an own function
             <span class="methodname">getDate($date, $locale)</span> which parses a date and returns the
            proper and normalized date parts. A monthname for example will be recognised and
            returned just a month number. But as <span class="classname">Zend_Locale</span> does not know
            anything about dates because it is a normalizing and localizing class we have
            integrated an own function  <span class="methodname">isDate($date)</span> which checks this.
        </p>

        <p class="para">
             <span class="methodname">isDate($date, $format, $locale)</span> can take up to 3 parameters
            and needs minimum one parameter. So what we need to verify a date is, of course, the
            date itself as string. The second parameter can be the format which the date is
            expected to have. If no format is given the standard date format from your locale is
            used. For details about how formats should look like see the chapter about <a href="zend.date.constants.html#zend.date.constants.selfdefinedformats" class="link">self defined formats</a>.
        </p>

        <p class="para">
            The third parameter is also optional as the second parameter and can be used to give a
            locale. We need the locale to normalize monthnames and daynames. So with the third
            parameter we are able to recognise dates like &#039;<strong class="command">01.Jänner.2000</strong>&#039; or
            &#039;<strong class="command">01.January.2000</strong>&#039; depending on the given locale.
        </p>

        <p class="para">
             <span class="methodname">isDate()</span> of course checks if a date does exist.
            <span class="classname">Zend_Date</span> itself does not check a date. So it is possible to
            create a date like &#039;<strong class="command">31.February.2000</strong>&#039; with
            <span class="classname">Zend_Date</span> because <span class="classname">Zend_Date</span> will
            automatically correct the date and return the proper date. In our case
            &#039;<strong class="command">03.March.2000</strong>&#039;.  <span class="methodname">isDate()</span> on the
            other side does this check and will return <b><tt>FALSE</tt></b> on
            &#039;<strong class="command">31.February.2000</strong>&#039; because it knows that this date is impossible.
        </p>

        <div class="example" id="zend.date.additional.checking.example-1"><div class="info"><p><b>Example #1 Checking Dates</b></p></div>
            

            <pre class="programlisting brush: php">
// Checking dates
$date = &#039;01.03.2000&#039;;
if (Zend_Date::isDate($date)) {
    print &quot;String $date is a date&quot;;
} else {
    print &quot;String $date is NO date&quot;;
}

// Checking localized dates
$date = &#039;01 February 2000&#039;;
if (Zend_Date::isDate($date,&#039;dd MMMM yyyy&#039;, &#039;en&#039;)) {
    print &quot;String $date is a date&quot;;
} else {
    print &quot;String $date is NO date&quot;;
}

// Checking impossible dates
$date = &#039;30 February 2000&#039;;
if (Zend_Date::isDate($date,&#039;dd MMMM yyyy&#039;, &#039;en&#039;)) {
    print &quot;String $date is a date&quot;;
} else {
    print &quot;String $date is NO date&quot;;
}
</pre>

        </div>
    </div>

    <div class="section" id="zend.date.additional.sunrise-sunset"><div class="info"><h1 class="title">Sunrise and Sunset</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Date</span> has also functions integrated for getting informations
            from the sun. Often it is necessary to get the time for sunrise or sunset within a
            particularly day. This is quite easy with <span class="classname">Zend_Date</span> as just the
            expected day has to be given and additionally location for which the sunrise or sunset
            has to be calculated.
        </p>

        <p class="para">
            As most people do not know the location of their city we have also spent a helper class
            which provides the location data for about 250 capital and other big cities around the
            whole world. Most people could use cities near themself as the difference for locations
            situated to each other can only be measured within some seconds.
        </p>

        <p class="para">
            For creating a listbox and choosing a special city the function
             <span class="methodname">Zend_Date_Cities::getCityList()</span> can be used. It returns the
            names of all available predefined cities for the helper class.
        </p>

        <div class="example" id="zend.date.additional.sunrise-sunset.example-1"><div class="info"><p><b>Example #2 Getting all Available Cities</b></p></div>
            

            <pre class="programlisting brush: php">
// Output the complete list of available cities
print_r (Zend_Date_Cities::getCityList());
</pre>

        </div>

        <p class="para">
            The location itself can be received with the
             <span class="methodname">Zend_Date_Cities::city()</span> function. It accepts the name of the
            city as returned by the  <span class="methodname">Zend_Date_Cities::getCityList()</span>
            function and optional as second parameter the horizon to set.
        </p>

        <p class="para">
            There are 4 defined horizons which can be used with locations to receive the exact time
            of sunset and sunrise. The &#039;<var class="varname">$horizon</var>&#039; parameter is always optional in
            all functions. If it is not set, the &#039;<span class="property">effective</span>&#039; horizon is used.
        </p>

        <table id="zend.date.additional.sunrise-sunset.table" class="doctable table"><div class="info"><caption><b>Types of Supported Horizons for Sunset and Sunrise</b></caption></div>
            

            
                <thead valign="middle">
                    <tr valign="middle">
                        <th>Horizon</th>
                        <th>Description</th>
                        <th>Usage</th>
                    </tr>

                </thead>


                <tbody valign="middle" class="tbody">
                    <tr valign="middle">
                        <td align="left">effective</td>
                        <td align="left">Standard horizon</td>

                        <td align="left">
                            Expects the world to be a ball. This horizon is always used if non is
                            defined.
                        </td>
                    </tr>


                    <tr valign="middle">
                        <td align="left">civil</td>
                        <td align="left">Common horizon</td>
                        <td align="left">Often used in common medias like TV or radio</td>
                    </tr>


                    <tr valign="middle">
                        <td align="left">nautic</td>
                        <td align="left">Nautic horizon</td>
                        <td align="left">Often used in sea navigation</td>
                    </tr>


                    <tr valign="middle">
                        <td align="left">astronomic</td>
                        <td align="left">Astronomic horizon</td>
                        <td align="left">Often used for calculation with stars</td>
                    </tr>

                </tbody>
            
        </table>


        <p class="para">
            Of course also a self-defined location can be given and calculated with. Therefor a
            &#039;<span class="property">latitude</span>&#039; and a &#039;<span class="property">longitude</span>&#039; has to be given
            and optional the &#039;<span class="property">horizon</span>&#039;.
        </p>

        <div class="example" id="zend.date.additional.sunrise-sunset.example-2"><div class="info"><p><b>Example #3 Getting the Location for a City</b></p></div>
            

            <pre class="programlisting brush: php">
// Get the location for a defined city
// uses the effective horizon as no horizon is defined
print_r (Zend_Date_Cities::city(&#039;Vienna&#039;));

// use the nautic horizon
print_r (Zend_Date_Cities::city(&#039;Vienna&#039;, &#039;nautic&#039;));

// self definition of a location
$mylocation = array(&#039;latitude&#039; =&gt; 41.5, &#039;longitude&#039; =&gt; 13.2446);
</pre>

        </div>

        <p class="para">
            As now all needed data can be set the next is to create a
            <span class="classname">Zend_Date</span> object with the day where sunset or sunrise should be
            calculated. For the calculation there are 3 functions available. It is possible to
            calculate sunset with &#039; <span class="methodname">getSunset()</span>&#039;, sunrise with
            &#039; <span class="methodname">getSunrise()</span>&#039; and all available informations related to the
            sun with &#039; <span class="methodname">getSunInfo()</span>&#039;. After the calculation the
            <span class="classname">Zend_Date</span> object will be returned with the calculated time.
        </p>

        <div class="example" id="zend.date.additional.sunrise-sunset.example-3"><div class="info"><p><b>Example #4 Calculating Sun Information</b></p></div>
            

            <pre class="programlisting brush: php">
// Get the location for a defined city
$city = Zend_Date_Cities::city(&#039;Vienna&#039;);

// create a date object for the day for which the sun has to be calculated
$date = new Zend_Date(&#039;10.03.2007&#039;, Zend_Date::ISO_8601, &#039;de&#039;);

// calculate sunset
$sunset = $date-&gt;getSunset($city);
print $sunset-&gt;get(Zend_Date::ISO_8601);

// calculate all sun informations
$info = $date-&gt;getSunInfo($city);
foreach ($info as $sun) {
    print &quot;\n&quot; . $sun-&gt;get(Zend_Date::ISO_8601);
}
</pre>

        </div>
    </div>

    <div class="section" id="zend.date.additional.timezones"><div class="info"><h1 class="title">Time Zones</h1></div>
        

        <p class="para">
            Time zones are as important as dates themselves. There are several time zones depending
            on where in the world a user lives. So working with dates also means to set the proper
            timezone. This may sound complicated but it&#039;s easier as expected. As already mentioned
            in the first chapter of <span class="classname">Zend_Date</span> the default timezone has to be
            set. Either by <var class="filename">php.ini</var> or by definition within the bootstrap file.
        </p>

        <p class="para">
            A <span class="classname">Zend_Date</span> object of course also stores the actual timezone.
            Even if the timezone is changed after the creation of the object it remembers the
            original timezone and works with it. It is also not necessary to change the timezone
            within the code with <acronym class="acronym">PHP</acronym> functions. <span class="classname">Zend_Date</span>
            has two built-in functions which makes it possible to handle this.
        </p>

        <p class="para">
             <span class="methodname">getTimezone()</span> returns the actual set timezone of within the
            <span class="classname">Zend_Date</span> object. Remember that <span class="classname">Zend_Date</span>
            is not coupled with <acronym class="acronym">PHP</acronym> internals. So the returned timezone is not
            the timezone of the <acronym class="acronym">PHP</acronym> script but the timezone of the object.
             <span class="methodname">setTimezone($zone)</span> is the second function and makes it possible
            to set new timezone for <span class="classname">Zend_Date</span>. A given timezone is always
            checked. If it does not exist an exception will be thrown. Additionally the actual
            scripts or systems timezone can be set to the date object by calling
             <span class="methodname">setTimezone()</span> without the zone parameter. This is also done
            automatically when the date object is created.
        </p>

        <div class="example" id="zend.date.additional.timezones.example-1"><div class="info"><p><b>Example #5 Working with Time Zones</b></p></div>
            

            <pre class="programlisting brush: php">
// Set a default timezone... this has to be done within the bootstrap
// file or php.ini.
// We do this here just for having a complete example.
date_default_timezone_set(&#039;Europe/Vienna&#039;);

// create a date object
$date = new Zend_Date(&#039;10.03.2007&#039;, Zend_Date::DATES, &#039;de&#039;);

// view our date object
print $date-&gt;getIso();

// what timezone do we have ?
print $date-&gt;getTimezone();

// set another timezone
$date-&gt;setTimezone(&#039;America/Chicago&#039;);

// what timezone do we now have ?
print $date-&gt;getTimezone();

// see the changed date object
print $date-&gt;getIso();
</pre>

        </div>

        <p class="para">
            <span class="classname">Zend_Date</span> always takes the actual timezone for object creation as
            shown in the first lines of the example. Changing the timezone within the created object
            also has an effect to the date itself. Dates are always related to a timezone. Changing
            the timezone for a <span class="classname">Zend_Date</span> object does not change the time of
            <span class="classname">Zend_Date</span>. Remember that internally dates are always stored as
            timestamps and in <acronym class="acronym">GMT</acronym>. So the timezone means how much hours should be
            substracted or added to get the actual global time for the own timezone and region.
        </p>

        <p class="para">
            Having the timezone coupled within <span class="classname">Zend_Date</span> has another positive
            effect. It is possible to have several dates with different timezones.
        </p>

        <div class="example" id="zend.date.additional.timezones.example-2"><div class="info"><p><b>Example #6 Multiple Time Zones</b></p></div>
            

            <pre class="programlisting brush: php">
// Set a default timezone... this has to be done within the bootstrap
// file or php.ini.
// We do this here just for having a complete example.
date_default_timezone_set(&#039;Europe/Vienna&#039;);

// create a date object
$date = new Zend_Date(&#039;10.03.2007 00:00:00&#039;, Zend_Date::ISO_8601, &#039;de&#039;);

// view our date object
print $date-&gt;getIso();

// the date stays unchanged even after changeing the timezone
date_default_timezone_set(&#039;America/Chicago&#039;);
print $date-&gt;getIso();

$otherdate = clone $date;
$otherdate-&gt;setTimezone(&#039;Brazil/Acre&#039;);

// view our date object
print $otherdate-&gt;getIso();

// set the object to the actual systems timezone
$lastdate = clone $date;
$lastdate-&gt;setTimezone();

// view our date object
print $lastdate-&gt;getIso();
</pre>

        </div>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.date.constants.html">Constants for General Date Functions</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.date.html">Zend_Date</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.db.html">Zend_Db</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.date.html">Zend_Date</a></li>
  <li><a href="zend.date.introduction.html">Introduction</a></li>
  <li><a href="zend.date.definition.theory.html">Theory of Operation</a></li>
  <li><a href="zend.date.basic.html">Basic Methods</a></li>
  <li><a href="zend.date.overview.html">Zend_Date API Overview</a></li>
  <li><a href="zend.date.creation.html">Creation of Dates</a></li>
  <li><a href="zend.date.constants.html">Constants for General Date Functions</a></li>
  <li class="active"><a href="zend.date.additional.html">Working Examples</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>